package cn.txun.springboot42.controller;

import cn.txun.springboot42.entity.User;
import cn.txun.springboot42.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/check")
    public int check(String username){
        User user = userMapper.selectByUsername(username);
        return user==null?1:2;//1为可用，2为不可用
    }
    @RequestMapping("/reg")
    public int reg(User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if(u==null){
            userMapper.insert(user);
            return 1;
        }
        return 2;//用户名已存在
    }
    @RequestMapping("/login")
    public int login(User user){
        User u = userMapper.selectByUsername(user.getUsername());
        if(u!=null){//根据用户输入用户名查到了数据
            if(u.getPassword().equals(user.getPassword())){
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户名错误
    }
}
